package com.cqeec.waimai.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cqeec.waimai.common.R;
import com.cqeec.waimai.entity.Employee;
import com.cqeec.waimai.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee,HttpServletRequest request){
//        获取用户输入的密码，并且进行md5加密
        String password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
//                      获取用户名这个字段       用户输入的用户名
        wrapper.eq(Employee::getUsername,employee. getUsername());
//        根据用户名进行查询，将查询结果保存
        Employee one = employeeService.getOne(wrapper);
//        System.out.println(one);
        if (one == null){
            return R.error("用户不存在");
        }
        if (!one.getPassword().equals(password)) {
            return R.error("密码错误");
        }if (one.getStatus()==0){
            return R.error("账户被禁用");
        }
        request.getSession().setAttribute("employee",one.getId());
        return R.success(one);
    }
    /*
    *       'url': '/employee/logout',
    *       'method' : 'post',
    *       注销功能
    * */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        request.getSession().removeAttribute("emplpyee");
        return R.success("退出");
    }
    /*
    * 分页模糊查询
    *   url : '/employee/page',
    *   method : 'get',
    * */
    @GetMapping("/page")
    public R<Page> pageR(int page,int pageSize,String name){
        Page<Employee> pageInfo = new Page<>(page,pageSize);
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
//        模糊查询  搜索框中，可以为空，可以不为空 获取字段名name   y=用户搜索的名字
        wrapper.like(name!=null,Employee::getName,name);
        employeeService.page(pageInfo,wrapper);
        return R.success(pageInfo);
    }
    /*
    * 添加员工
    * */
    @PostMapping
    public R<String> addEmployee(@RequestBody Employee employee){
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employeeService.save(employee);
        return R.success("新增员工成功");
    }
    /*
    * 修改------数据回显
    * */
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable("id")Long id){
        Employee byId = employeeService.getById(id);
        return R.success(byId);
    }
    /*
    * 修改
    * */
    @PutMapping
    public R<String> updateByEntity(@RequestBody Employee employee) {
        employeeService.updateById(employee);
        return R.success("修改成功");
    }
}